package cate.game.activity.bosstrain;

import cate.common.table.activity.bosstrain.row.BossConfigRow;
import cate.common.table.activity.bosstrain.row.ChallengeRewardRow;
import cate.common.table.d.GDFight;
import cate.common.table.d.GDFunc;
import cate.common.table.d.GDOperation;
import cate.common.util.GameResult;
import cate.common.util.TimeTool;
import cate.game.activity.base.core.SubActivitySingle;
import cate.game.activity.bosstrain.msg.BossTrainFightInfoResp;
import cate.game.fight.task.FightTask;
import cate.game.mail.po.MailBuilder;
import cate.game.play.part.FightSide;
import cate.game.play.proce.play.PlayPO;
import cate.game.res.MixRes;
import cate.game.role.farm.IFarm;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

public class BossTrainFight extends SubActivitySingle<BossTrainActivity> implements IFarm {
    @NoteField("今日最高伤害")
    public double damage;
    @NoteField("下一次结算时间")
    public long nextCheckTime;
    @NoteField(value = "战斗")
    public boolean fighting;

    @Override
    public void initialize(BossTrainActivity parent) {
        super.initialize(parent);
        FightTask task = role.getGame().fight.task.getTask(role, getFightFuncId());
        fighting = task != null && !task.invalid();
    }

    @Override
    public void onEnter() {
        super.onEnter();
        FightTask task = role.getGame().fight.task.getTask(role, getFightFuncId());
        fighting = task != null && !task.invalid();
    }

    @Override
    public void onDaySpan(boolean silence) {
        super.onDaySpan(silence);
    }

    public GameResult<BossTrainFight> challenge() {
        GameResult<BossTrainFight> r = new GameResult<>();
        r.data = this;
        if (isFighting()) {
            return r.fail("当前战斗未结束");
        }
        BossConfigRow row = role.getGame().table.bossTrain.config.findConfigRow(parent.configTid());
        if (row == null) {
            return r.fail("配置不存在!");
        }
        EcResult<FightSide> fsRes = role.getGame().fight.side.createFightSideByCode(GDFight.Team.B, row.monster);
        if (!fsRes.ok()) {
            return r.fail(fsRes.message);
        }
        BossTrainFTC context = new BossTrainFTC(role, funcId());
        context.funcId = getFightFuncId();
        context.setPositionApply(GDFunc.ADVENTURE);
        EcResult<PlayPO> fightRes = role.getGame().play.startByRole(role, context, fsRes.data);
        if (!fightRes.ok()) {
            return r.fail(fightRes.message);
        }
        role.getHistory().action.bossTrainTimes();
        this.startFighting();
        return r.success();
    }

    public void tick() {
        if (nextCheckTime == 0 || now() < nextCheckTime) {
            return;
        }
        if (TimeTool.isDifferentDay(now(), nextCheckTime) ) {
            if (now() < TimeTool.getRealTimestamp("21:00:00")) {
                nextCheckTime = TimeTool.calTimeStamp(now(), 1, "21:00:00");
            }else {
                nextCheckTime = TimeTool.calTimeStamp(now(), 2, "21:00:00");
            }
        } else {
            nextCheckTime = TimeTool.calTimeStamp(now(), 2, "21:00:00");
        }
        //发奖励
        ChallengeRewardRow row = role.getGame().table.bossTrain.reward.findRowByDamage(this.damage, parent.configTid());
        if (row == null) {
            return;
        }
        this.damage = 0;
        MixRes reward = new MixRes(row.reward);
        if (row.mailId == 0) {
            return;
        }
        sendMail(reward, row.mailId);
    }

    private void sendMail(MixRes reward, int mailID) {
        MailBuilder mailBuilder = new MailBuilder();
        mailBuilder.setContentId(mailID).setReward(reward.copy())
                .setOperation(GDOperation.BOSS_TRAIN_CHALLENGE);
        role.getMail().add(mailBuilder.build());
    }

    public void updateDamage(double damage) {
        if (damage > this.damage) {
            this.damage = damage;
        }
        role.getHistory().action.bossTrainDamage((long) damage);
        noticeUpdate();
    }

    public void onRoleOpen() {
        long nine = TimeTool.getRealTimestamp("21:00:00");
        if (now() >= nine) {
            nextCheckTime = TimeTool.calTimeStamp(now(), 2, "21:00:00");
            ;
        } else {
            nextCheckTime = nine;
        }
        this.damage = 0;
        this.fighting = false;
    }

    public void onRoleEnd() {
        nextCheckTime = 0;
        this.damage = 0;
        this.fighting = false;
    }

    @Override
    public boolean isFighting() {
        return fighting;
    }

    @Override
    public int getFightFuncId() {
        return GDFunc.BOSS_TRAIN;
    }

    @Override
    public void startFighting() {
        this.fighting = true;
    }

    @Override
    public void endFighting() {
        this.fighting = false;
    }

    public void noticeUpdate() {
        role.sendNow(new BossTrainFightInfoResp(this));
    }
}
